package com.csy.property.community.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csy.commons.dto.SysUser;
import com.csy.commons.vo.AjOwnerVo;
import com.csy.feign.user.api.UserServiceApi;
import com.csy.property.community.domain.AjCommunity;
import com.csy.property.community.domain.AjFacility;
import com.csy.property.community.domain.vo.AjFacilityWhRecordVo;
import com.csy.property.community.mapper.AjCommunityMapper;
import com.csy.property.community.mapper.AjFacilityMapper;
import com.csy.property.community.service.IAjFacilityService;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2025-01-23
 */
@Service
@Slf4j
public class AjFacilityServiceImpl extends ServiceImpl<AjFacilityMapper, AjFacility> implements IAjFacilityService {

    @Autowired
    AjFacilityMapper ajFacilityMapper;

    @Resource
    private AjCommunityMapper ajCommunityMapper;

    @Resource
    private UserServiceApi userServiceApi;


    /**
     * 获取设施记录
     * @param ajFacility
     * @return
     */
    @Override
    public List<AjFacility> getFacilityList(AjFacility ajFacility) {
        List<AjFacility> list = ajFacilityMapper.getFacilityList(ajFacility);
        return list;
    }

    /**
     * 查询设施维护记录
     * @param facilityId
     * @return
     */
    @Override
    public List<AjFacilityWhRecordVo> getFacilityWhRecords(Integer facilityId) {
        List<AjFacilityWhRecordVo> list = ajFacilityMapper.getFacilityWhRecords(facilityId);
        return list;
    }

    /**
     * 添加设施
     * @param ajFacility
     * @return
     */
    @Override
    public int add(AjFacility ajFacility) {
        try {
            String managerName = ajFacility.getManagerName();
            String ownerName = ajFacility.getOwnerName();
            AjCommunity ajCommunity = ajCommunityMapper.selectById(ajFacility.getCommunityId());
            ajFacility.setCommunityName(ajCommunity.getCommunityName());
            SysUser user = userServiceApi.getByName(managerName);
            if (BeanUtil.isEmpty(user)){
                return 0;
            }
            Long userId = user.getUserId();
            ajFacility.setManagerId(userId.toString());
            AjOwnerVo ownerVo = userServiceApi.getOwnerByName(ownerName);
            if (BeanUtil.isEmpty(ownerVo)){
                return 0;
            }
            Long ownerId = ownerVo.getOwnerId();
            ajFacility.setOwnerId(ownerId.toString());
            return ajFacilityMapper.insert(ajFacility);
        }catch (Exception e){
            log.error("插入设施失败",e);
        }
        return 0;
    }

    /**
     * 添加维护记录
     * @param recordVo
     * @return
     */
    @Override
    public int addWH(AjFacilityWhRecordVo recordVo) {
        recordVo.setCreateBy("admin");
        recordVo.setCreateTime(new Date());
        return ajFacilityMapper.addWH(recordVo);
    }
}
